package com.practice.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

@RestController
public class newdemo {
    @RequestMapping("/happy")
    public String happy(String username,Integer age){
        System.out.println("接收到的参数是"+username+"年龄"+age);
        return "接收到的参数是"+username+age;
    }
    @PostMapping("/happy1")
    public String happy1(){
        return "222222";
    }
    @PostMapping("/happy2")
    public String happy2(){
        return "333333";
    }
    @RequestMapping("/happy3")
    public demo1 happy3(demo1 demo){
        return demo;
    }
    @RequestMapping("/happy4")
    public String happy4(@RequestParam(value = "username",required =false)
                             String name, int age){
        return name+age;
    }
    @RequestMapping("/happy5")
    public String happy5(String[] demo){
        return "返回的数组是"+ demo[0];
    }
    @RequestMapping("/happy6")

    public String happy6(int[] demo){
        return "返回的数组是"+ Arrays.toString(demo);
    }
    @PostMapping("/happy7/{articleId}")
    public String happy7(@PathVariable("articleId")Integer articleId){
        return "获取到的articleId:"+articleId;

    }
    @PostMapping("/happy8/{schoolId}/{school}")
    public String happy8(@PathVariable("schoolId")Integer schoolId,
                         @PathVariable("school")String school){
        return "获取到的schoolId:"+schoolId+",school:"+school;
    }
    @PostMapping("/happy9")
    public String happy9(@RequestPart("file2")MultipartFile file1) throws IOException {
//        System.out.println("file.getContentType():"+file.getContentType());
//        System.out.println("file.getBytes():"+file.getBytes());
//        System.out.println("file.toString():"+file.toString());
        System.out.println("file.getName():"+file1.getName());
        System.out.println(file1.getOriginalFilename());
//        System.out.println("file.getResource():"+file.getResource());
//        System.out.println("file.isEmpty():"+(file.isEmpty()));
        file1.transferTo(new File("D:/demo-practice/"+file1.getOriginalFilename()));
        return "接收到的文件:"+file1.getOriginalFilename();
    }
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies=request.getCookies();
        System.out.println("获取Cookie的值");
        if (cookies!=null){
            for (Cookie cookie:cookies){
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }else{
            System.out.println("cookie为null");
        }
        return "获取Cookie成功" ;
    }
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("qqq")String a){
        System.out.println("从Cookie中获取:"+a);
        return "随便";
    }
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
       HttpSession session=request.getSession(true);
       session.setAttribute("name","zhangsan");
       session.setAttribute("password","123456");
        return "获取Session成功";
    }
    @RequestMapping("/getSession1")
    public String getSession1(HttpServletRequest request){
        HttpSession session =request.getSession();
        System.out.println("name:"+session.getAttribute("name"));
        System.out.println("password:"+session.getAttribute("password"));
        return "获取Session成功";
    }
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        System.out.println("name:"+session.getAttribute("name"));
        System.out.println("password:"+session.getAttribute("password"));
        return "获取Session成功";
    }
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value ="name",required = false) String a){
        System.out.println("name:"+a);

//        System.out.println("password:"+b);
        return "获取Session成功";
    }
}

